<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<%@ include file="console_element/top.jsp" %>
<script type="text/javascript" src="../js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="../js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    window.onload = function () {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                var data = JSON.parse(this.responseText).data;
                console.log(data);

                var select = document.getElementById("topics");
                select.innerText = '';

                for (var i = 0; i < data.length; i++) {
                    var option = document.createElement("option");
                    option.setAttribute("value", data[i].tid);
                    var text = document.createTextNode(data[i].tname);
                    option.appendChild(text);
                    select.appendChild(option);
                }
            }
        }
        xhttp.open("GET", "<%=basePath%>topic/list/data", true);
        xhttp.send();

        (function () {
            var result = "${result}";
            if (result) {
                alert(result);
            }
        })()

        $(function () {
            $("#file").change(function (e) {
                var file = e.target.files[0];
                var reader = new FileReader();
                reader.onloadend = function () {
                    $("#file_img").attr("src", reader.result)
                }
                if (file) reader.readAsDataURL(file);
            })
        })
    };

    function check() {
        var ntitle = document.getElementById("ntitle");
        var nauthor = document.getElementById("nauthor");
        var nsummary = document.getElementById("nsummary");
        var ncontent = document.getElementById("ncontent");

        if (ntitle.value == "") {
            alert("标题不能为空！！");
            ntitle.focus();
            return false;
        } else if (nauthor.value == "") {
            alert("作者不能为空！！");
            nauthor.focus();
            return false;
        } else if (nsummary.value == "") {
            alert("摘要不能为空！！");
            nsummary.focus();
            return false;
        } else if (ncontent.value == "") {
            alert("内容不能为空！！");
            ncontent.focus();
            return false;
        }
        return true;
    }
</script>

<div id="main">
    <%@ include file="console_element/left.jsp" %>
    <div id="opt_area">
        <h1 id="opt_type"> 添加新闻： </h1>
        <form action="<%=basePath%>news/add" onsubmit="return check()" method="post"
              enctype="multipart/form-data">
            <p>
                <label for="topics"> 主题 </label>
                <select id="topics" name="ntid">
                    <%--                            <c:forEach items="${requestScope.topics}" var="topic">--%>
                    <%--                                 <option value='${topic.tid}'>${topic.tname}</option> --%>
                    <%--                            </c:forEach>--%>
                </select>
            </p>
            <p>
                <label for="ntitle"> 标题 </label>
                <input id="ntitle" name="ntitle" type="text" class="opt_input"/>
            </p>
            <p>
                <label for="nauthor"> 作者 </label>
                <input id="nauthor" name="nauthor" type="text" class="opt_input"/>
            </p>
            <p>
                <label for="nsummary"> 摘要 </label>
                <textarea id="nsummary" name="nsummary" cols="40" rows="3"></textarea>
            </p>
            <p>
                <label for="ncontent"> 内容 </label>
                <textarea class="ckeditor" id="ncontent" name="ncontent" cols="70" rows="10"></textarea>
            </p>
            <p>
                <label for="file"> 上传图片 </label>
                <input id="file" name="file" type="file" class="opt_input"/>
                <img id="file_img" height="200">
            </p>
            <input type="submit" value="提交" class="opt_sub"/>
            <input type="reset" value="重置" class="opt_sub"/>
        </form>
    </div>
</div>

<%@ include file="console_element/bottom.html" %>
